let express = require('express');
let router = express.Router();
let filter = require('../../components/filter');

/**
 *
 * 按id查看指定的网站信息信息
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from info", [], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": true, "data": undefined});
        }
        else {
            res.json({"success": true, "data": results[0]});
        }
    });
});

/**
 * 修改网站信息数据 by p 2018-06-29
 *
 *
 */
router.put('/:id', filter.authorize, function (req, res, next) {
    let params = req.body;

    if (params.title === undefined || params.title === '') {
        res.json({"success": false, "message": "网站标题能为空！"});
        return;
    }

    if (params.content === undefined || params.content === '') {
        res.json({"success": false, "message": "网站内容能为空！"});
        return;
    }

    req.app.db.query("update info set title = ? content = ?", [params.title, params.content], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.putMsg});
    });
});

/**
 * 新增网站信息数据 by p 2018-06-11
 *
 *
 */
router.post('/', filter.authorize, function (req, res, next) {
    let params = req.body;

    if (params.content === undefined || params.content === '') {
        res.json({"success": false, "message": "网站内容能为空！"});
        return;
    }

    req.app.db.query("update info set content = ?", [params.content], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.postMsg});
    });
});

module.exports = router;